/*
 * user : hxx
 * 
 * time : 2018-01-15
 * 
 * 此类将运行的对象添加入库，每个类保持一份，
 * 
 * tostring 会自动将该类中的变量，公共，静态，及有公共方法可见的变量，串化字符串返回；
 * 
 * 及时的查看，并且可以修改该类变量的值；
 * 
 * Lutra： 主要类
 */



package com.hxx.lib;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.net.URL;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


public class Lutra {
	
	private  static  ConcurrentHashMap<String, Object> ojmap = new ConcurrentHashMap<String, Object>();
		
	//add one Object	
	public static void addOj(Object oj){	
		String key= oj.getClass().getName();
		ojmap.put(key, oj);		
	}
	
	//get one Object  : key is class name
	public static Object getOj(String classname){	
		Object value =null;		
		if (classname==null ||  classname.isEmpty())
			return value;
		
		for (Map.Entry<String, Object> entry : ojmap.entrySet()) {
			String pkey = entry.getKey();
			if (pkey.startsWith(classname)){ 
				value=entry.getValue();
				break;		
			}
		}	
		return value;
	}
	
	//get Object Map :  key is package
	public static ConcurrentHashMap<String, Object> getOjMap(String key){	
		Object value =null;
		ConcurrentHashMap<String, Object> retmap =new ConcurrentHashMap<String, Object>();
				
		if (key==null ||  key.isEmpty())
			return retmap;
		
		if  (key.equalsIgnoreCase("all")){		
			return ojmap;
		}		
		for (Map.Entry<String, Object> entry : ojmap.entrySet()) {
			String pkey = entry.getKey();
			if (pkey.startsWith(key)){ 
				value=entry.getValue();
				retmap.put(pkey, value);				
			}
		}
		return retmap;
	}
	
	private static URL getClassType(String classname){		
		String newclassname=classname.replace(".", "/")+".class";
		URL pn=Thread.currentThread().getContextClassLoader().getResource(newclassname);			
		return pn;
	}
	
	private static int getUrlType(URL pn){
		int ret=0;
		if (pn!=null){	
			String head=pn.getProtocol();
			if ("file".equals(head)){				
				ret=1;
			}else if ("jar".equals(head)){				
				ret=2;
			}		
		}
		return ret;
	}
	
	
	public static String classnameToString(String classname){	
		Object oj= getOj(classname);
		if (oj==null)
			return   null;
		return  obToString(oj);
	}
	
	
	//Object to String  for  all (superclass  or  subclass)
 	public static String obToString(Object oj){		
		Object val=null;
		String nfx="\n";
		StringBuffer  sBuffer =null;
		
		if (oj==null){			
			return null;
		}
			
		sBuffer = new StringBuffer(oj.toString()+":");	
		Class<?> clazz = oj.getClass();
		
		for(;clazz!=Object.class; clazz=clazz.getSuperclass()){	
			sBuffer.append(nfx);
			sBuffer.append("    ");
			sBuffer.append(clazz.getName());			
			
			Field[] fields = clazz.getDeclaredFields();
			Method[] methods= clazz.getDeclaredMethods();
			
			for (int i = 0; i < fields.length; i++) {
				String newkey=fields[i].getName();	
				String classname=fields[i].getType().getName();			
				ClassLoader cl= fields[i].getType().getClassLoader();	
			
				try {
					val = fields[i].get(oj);
				} catch (IllegalArgumentException e2) {
					val=null;
					// TODO Auto-generated catch block
					//e2.printStackTrace();
				} catch (IllegalAccessException e2) {
					// TODO Auto-generated catch block
					//e2.printStackTrace();
					val=null;
				}			
				
				if (val==null){				
					
					for(int j=0;j<methods.length;j++){
					  String  mname=methods[j].getName();		  
					  Parameter[] gp=methods[j].getParameters();
					  
					  if (gp.length>0)			  
						  continue;
					  
					  if (mname.equalsIgnoreCase("get"+newkey)){						  
							try {
								val=methods[j].invoke(oj);
								break;
							} catch (IllegalAccessException e) {
								// TODO Auto-generated catch block
								val=null;
								//e.printStackTrace();
							} catch (IllegalArgumentException e) {
								val=null;
								// TODO Auto-generated catch block
								//e.printStackTrace();
							} catch (InvocationTargetException e) {
								val=null;
								// TODO Auto-generated catch block
								//e.printStackTrace();
							}				
						}
					}
				}				
				
				sBuffer.append("{变量名:");
				sBuffer.append(newkey);
				sBuffer.append(",类型:");
				sBuffer.append(fields[i].getType().getName());
				sBuffer.append(",值:");
				sBuffer.append(val);
				sBuffer.append("}");
				
				if (cl!=null && val!=null){
					
					URL pn=getClassType(classname);
					
					int cty=getUrlType(pn);
					
					if (cty==1){
						sBuffer.append(nfx);
						sBuffer.append("    ");
						sBuffer.append(obToString(val));						
					}else if(cty==2){
						sBuffer.append(nfx);
						sBuffer.append("    ");
						sBuffer.append(pn);
					}
				}
			}	
		}
		return sBuffer.toString();
	}	
 	 	
}
